package com.cskaoyan.controller.backStage;

import com.cskaoyan.model.Coupon;
import com.cskaoyan.model.CouponUser;
import com.cskaoyan.model.bo.CouponListBO;
import com.cskaoyan.model.bo.CouponUserListBO;
import com.cskaoyan.model.vo.BaseRespVO;
import com.cskaoyan.model.vo.CouponInfoVO;
import com.cskaoyan.model.vo.DataVO;
import com.cskaoyan.model.vo.ExceptionVO;
import com.cskaoyan.service.CouponService;
import com.cskaoyan.util.ValidationUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @ClassName: CouponController
 * @Description: 优惠卷管理
 * @Author:YPS
 * @Date: 2022/5/6 11:51
 * @Version: 1.0
 **/
@RestController
@RequestMapping("admin/coupon")
public class CouponController {

    @Autowired
    CouponService couponService;
    //展示优惠券list
    @RequiresPermissions("admin:coupon:list")
    @RequestMapping("list")
    public BaseRespVO<CouponListBO> couponList(CouponListBO couponListBO){
        DataVO<CouponInfoVO> couponInfoVODataVO = couponService.queryCoupon(couponListBO);
        return BaseRespVO.ok(couponInfoVODataVO);
    }
    //新建优惠券
    @RequiresPermissions("admin:coupon:create")
    @RequestMapping("create")
    public ExceptionVO createCoupon(  @RequestBody  Coupon couponInfoVO)  {
        Date starttime = couponInfoVO.getStartTime();
        Date endtime = couponInfoVO.getEndTime();
        if (!(starttime == null || "".equals(starttime))&&!(endtime == null || "".equals(endtime))){
            if (couponInfoVO.getStartTime().after(couponInfoVO.getEndTime())){
                return ExceptionVO.errorprice("截止日期必须晚于起始日期");
            }
        }

        if (couponInfoVO.getDiscount().compareTo(couponInfoVO.getMin()) == -1){
            Coupon couponInfo = couponService.createCoupon(couponInfoVO);
            return BaseRespVO.ok(couponInfo);
        }
        return ExceptionVO.errorprice("满减金额必须小于最低消费");
    }
    //编辑优惠券信息
    @RequiresPermissions("admin:coupon:update")
    @RequestMapping("update")
    public ExceptionVO updateCoupon(  @RequestBody  Coupon couponInfoVO)  {
        Date starttime = couponInfoVO.getStartTime();
        Date endtime = couponInfoVO.getEndTime();
        if (!(starttime == null || "".equals(starttime))&&!(endtime == null || "".equals(endtime))){
            if (couponInfoVO.getStartTime().after(couponInfoVO.getEndTime())){
                return ExceptionVO.errorprice("截止日期必须晚于起始日期");
            }
        }
        if (couponInfoVO.getDiscount().compareTo(couponInfoVO.getMin()) == -1){
            CouponInfoVO couponInfo = couponService.updateCoupon(couponInfoVO);
            return BaseRespVO.ok(couponInfo);
        }
        return ExceptionVO.errorprice("满减金额必须小于最低消费");
    }
    //删除优惠券信息
    @RequiresPermissions("admin:coupon:delete")
    @RequestMapping("delete")
    public ExceptionVO deleteCoupon(@RequestBody Coupon couponInfoVO){
        couponService.deleteCoupon(couponInfoVO);
        return new ExceptionVO();
    }
    @RequiresPermissions("admin:coupon:read")
    @RequestMapping("read")
    public BaseRespVO<Coupon> readCoupon( Integer id){
        Coupon coupon = couponService.queryCouponById(id);
        return BaseRespVO.ok(coupon);
    }
    //
    @RequiresPermissions("admin:coupon:listuser")
    @RequestMapping("listuser")
    public BaseRespVO<DataVO<CouponUser>> userList(CouponUserListBO couponUser){
        DataVO<CouponUser> couponUserList = couponService.getUserList(couponUser);
        return BaseRespVO.ok(couponUserList);

    }
}
